package com.liukai.todo_android.common.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.liukai.todo_android.R;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/18 10:06 上午
 * @description：文字背景色工具类
 * @modified By：
 * @version: $
 */
public class TextViewBgUtil {

    public static void setTextBg(TextView view, String text, String str, Context context) {
        int bstart = text.indexOf(str);
        int bend = bstart + str.length();
        int fstart = text.indexOf(str);
        int fend = fstart + str.length();
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new BackgroundColorSpan(context.getResources().getColor(R.color.shallowMainBG)),
                bstart, bend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.black)),
                fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        view.setText(style);
    }

}
